﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Data;

namespace olierFrm.WebFormDev.StdQuery
{
    public class DataItemTemplate : ITemplate
    {

        public void InstantiateIn(System.Web.UI.Control container)
        {
            var dataItemContainer = ((DevExpress.Web.ASPxGridView.GridViewDataItemTemplateContainer)(container));
            var dtSource = (DataTable)dataItemContainer.Grid.DataSource;
            /*
            #region 处理隐藏字段
            var hideColumn = dtSource.GetHideColumn();
            if (hideColumn != null)
            {
                if (hideColumn.Contains(dataItemContainer.Column.FieldName)) return;
            }
            #endregion
            */
            var dataType = dtSource.Columns[dataItemContainer.Column.Index].DataType.Name;
            switch (dataType)
            {
                case "Boolean":

                    container.Controls.Add(new DevExpress.Web.ASPxEditors.ASPxCheckBox()
                    {
                        Enabled = false,
                        Checked = dataItemContainer.Text == "Unchecked" ? false : true
                    });
                    break;
                case "Decimal":
                    try
                    {
                        container.Controls.Add(new LiteralControl(Decimal.Parse(dataItemContainer.Text).ToString("N")));
                    }
                    catch { }
                    break;
                case "DateTime":
                    try
                    {
                        var rowIndex = ((DevExpress.Web.ASPxGridView.GridViewBaseRowTemplateContainer)(dataItemContainer)).VisibleIndex;
                        var columnIndex = dataItemContainer.Column.Index;
                        string value = dtSource.Rows[rowIndex][columnIndex].ToString();
                        string text = "";
                        DateTime outdt;
                        if (DateTime.TryParse(value, out outdt))
                        {
                            text = outdt.ToString("yyyy-MM-dd HH:mm:ss");
                            if (text.Split(' ')[1] == "00:00:00") text = text.Split(' ')[0];
                        }
                        container.Controls.Add(new LiteralControl(text));
                    }
                    catch { }

                    break;
                default:
                    container.Controls.Add(new LiteralControl(dataItemContainer.Text));
                    break;
            }

        }

    }
}
